package cn.stylefeng.guns.modular.system.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.system.entity.SysConfType;
import cn.stylefeng.guns.modular.system.service.SystemConfService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Controller
@RequestMapping("/systemConf")
public class SystemConfController extends BaseController {
	private String PREFIX = "/modular/system/systemConf/";
	@Autowired
	private SystemConfService systemConfService;
	
    /**
     * 跳转到系统配置管理首页
     * @return
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "systemConfPage.html";
    }
	
    /**
     * 获取所有系统配置列表
     */
    @RequestMapping(value = "/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(value = "name", required = false) String name,
    				   @RequestParam(value = "type", required = false) String type) {
    	Page<Map<String, Object>> list = systemConfService.list(name, type);
    	return LayuiPageFactory.createPageInfo(list);
    }
    
    @RequestMapping("systemConf_add")
    public String sysConfAdd() {
        return PREFIX + "systemConf_add.html";
    }
    
    
    @RequestMapping(value = "/add")
    @Permission
    @ResponseBody
    public ResponseData add(SysConfType sysConfType) {
    	systemConfService.addSysConfType(sysConfType);
        return SUCCESS_TIP;
    }
    
    @RequestMapping(value = "/isExist")
    @ResponseBody
    public ResponseData isExist(String type){
    	boolean typeIsExist = systemConfService.typeIsExist(type);
    	if (typeIsExist) {
    		return new ErrorResponseData("配置编码已经存在,请重新输入!");
		}else {
			return SUCCESS_TIP;
		}
    }
    
    
}
